function v = sllogdet(A)
%SLLOGDET Computes the log-determinant of the positive definite matrix
%
% [ Syntax ]
%   - v = sllogdet(A)
%
% [ Arguments ]
%   - A:        The input matrix, which must be positive definite
%   - v:        The logarithm of det(A)
%
% [ Description ]
%   - v = sllogdet(A) computes the logarithm of the determinant of a 
%     positive definite matrix A using a stable and effcient way.
% 
% [ Remarks ]
%   - It is implemented using two strategies: 
%       # It is based on Cholesky factorization of A
%       # It uses sum of logarithm instead of logarithm of product to 
%         reduce the risk of overflow.
%
%   - It only uses the lower triangular part of A.
%
% [ History ]
%   - Created by Dahua Lin, on Oct 18, 2007
%   - Modified by Dahua Lin, on Dec 19, 2007
%       - Now it accepts semi-definite matrix. For the rank-deficient
%         matrix, it returns -inf. (log (0)).
%       - Add argument checking
%


%% parse and verify input arguments

assert(isnumeric(A), ...
    'sltoolbox:sllogdet:invalidarg', ...
    'A should be a numeric matrix.');


%% main

[L, p] = chol(A, 'lower');

if p == 0
    v = sum(log(abs(diag(L)))) * 2;
else
    v = -inf;
end
